﻿using Infrastructure.Attribute;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.mes.md;
using ZR.Service.mes.md.IService;
using ZR.Service.MES.md.IService;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ZR.Service.MES.md
{

    [AppService(ServiceType = typeof(IMdBOMService), ServiceLifetime = LifeTime.Transient)]
    public class MdBOMService : BaseService<MdBom>, IMdBOMService
    {
        public int deleteBOM(int[] ids)
        {
            return Delete(ids);

        }

        public List<MdUnit> GetAllunitList()
        {
           return  Context.Queryable<MdUnit>().Where(it=>it.EnableFlag=='Y').ToList();
        }

        public int UpdateBOM(MdBom mdBom)
        {
           return Context.Updateable(mdBom).WhereColumns(it => new {it.Id, it.ProductCode }).ExecuteCommand();
        }

        int IMdBOMService.AddBom(MdBom bom)
        {
            return Insert(bom);
        }

       

        (int, List<MdBom>) IMdBOMService.GetAll(string productCode, string productName, int pageNum, int pageSize)
        {
            int totalNum = 0;
            if (string.IsNullOrEmpty(productCode) && string.IsNullOrEmpty(productName))
            {
                List<MdBom> data = Context.Queryable<MdBom>().ToTree(it => it.Child, it => it.ParentProductId, null);
                totalNum = data.Count();
                data = data.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();


                return (totalNum, data);


            }
            else
            {
                var predicate = Expressionable.Create<MdBom>()
               .AndIF(!string.IsNullOrEmpty(productCode), it => it.ProductCode.Contains(productCode))
               .AndIF(!string.IsNullOrEmpty(productName), it => it.ProductName.Contains(productName))
               .And(it=>it.ParentProductId==null)
               .ToExpression();

                List<MdBom> data = Context.Queryable<MdBom>().Where(predicate).ToList();
                totalNum = data.Count();
                List<MdBom> dataSons = new List<MdBom>();
                if (data != null && data.Count > 0)
                {
                    foreach (var item in data)
                    {
                        List<MdBom> data1 = Context.Queryable<MdBom>().ToTree(it => it.Child, it => it.ParentProductId, item.Id);
                        item.Child=data1;
                    }
                }
                data = data.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
                return (totalNum, data);

            }





        }

        List<MdBom> IMdBOMService.QueryBOM(string queryString)
        {
            var predicate = Expressionable.Create<MdBom>()
                .AndIF(!string.IsNullOrEmpty(queryString), it => it.ProductCode.Contains(queryString))
                .ToExpression();
           return Context.Queryable<MdBom>().Where(predicate).ToList();
        }

    

    }
}
